package com.github.postsanf.yinian.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.Toast;

import com.github.postsanf.yinian.BaseFragment;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.adapter.CoverThemeMoreAdapter;
import com.github.postsanf.yinian.bean.CoverTheme;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.entity.YNCoverThemeResponse;
import com.github.postsanf.yinian.utils.ReqUtils;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class CoverThemeFragment extends BaseFragment {

    private List<CoverTheme> tempUris = new ArrayList<>();
    private GridView mGridView;
    private CoverThemeMoreAdapter coverThemeMoreAdapter;

    public static Fragment newInstance(String arg) {
        CoverThemeFragment fragment = new CoverThemeFragment();
        Bundle bundle = new Bundle();
        bundle.putString("title", arg);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = getActivity().getLayoutInflater().inflate(R.layout.yn_fragment_covertheme, null);
        mGridView = (GridView) view.findViewById(R.id.covertheme_gdview);
        coverThemeMoreAdapter = new CoverThemeMoreAdapter(getActivity(), tempUris);
        mGridView.setAdapter(coverThemeMoreAdapter);
        initdata();
        return view;
    }

    private void initdata() {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put("theme", getArguments().getString("title"));
        StringRequest req = new StringRequest(getString(R.string.BASE_URL) + getString(R.string.GetThemePicture))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        YNCoverThemeResponse resAlertBg = gson.fromJson(s, YNCoverThemeResponse.class);
                        if (ReqUtils.isSuccess(resAlertBg.getCode())) {
                            tempUris.addAll(resAlertBg.getData());
                            coverThemeMoreAdapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        Toast.makeText(getActivity(),CommonConstants.YN_NET_LINK,Toast.LENGTH_SHORT).show();
                    }
                });
        liteHttp.executeAsync(req);
    }

    // 友盟统计
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("MainScreen");
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("MainScreen");
    }
}